#include <bits/stdc++.h>

using namespace std;

// #define LOCK_GETCHAR
// #define USE_INT_128

#if __cplusplus < 201400
#warning "Please use c++14 or higher."
#define CONSTEXPR_FUNC
#define ENABLE_IF_INT
#else
#define CONSTEXPR_FUNC constexpr
#define ENABLE_IF_INT , enable_if_t<_is_integer<T>, int> = 0
template <class T> constexpr bool _is_integer = numeric_limits<T>::is_integer;
template <> constexpr bool _is_integer<bool> = false;
template <> constexpr bool _is_integer<char> = false;
#ifdef USE_INT_128
template <> constexpr bool _is_integer<__int128> = true;
template <> constexpr bool _is_integer<__uint128_t> = true;
#endif
#endif

#if !defined(_WIN32) && !defined(LOCK_GETCHAR)
#define getchar getchar_unlocked
#endif

#define il inline
#define mkp make_pair
#define fi first
#define se second
#define For(i, j, k) for (decltype(j - k) i = (j); i <= (k); ++i)     // NOLINT
#define ForDown(i, j, k) for (decltype(j - k) i = (j); i >= (k); --i) // NOLINT
#define eb emplace_back
#ifndef ONLINE_JUDGE
#define FileIO(filename)                                                       \
  freopen(filename ".in", "r", stdin);                                         \
  freopen(filename ".out", "w", stdout)
#else
#define FileIO(filename) void(0)
#endif

using ll = long long;
using uint = unsigned int;
using ull = unsigned long long;
using db = double;
using ldb = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#ifdef USE_INT_128
using lll = __int128_t;
using ulll = __uint128_t;
#endif

// clang-format off
CONSTEXPR_FUNC il ll qpow(ll x, ull y, ll mod){ ll ans = 1; x %= mod; while (y) { if (y & 1) (ans *= x) %= mod; (x *= x) %= mod; y >>= 1; } return ans; }
template<typename T> CONSTEXPR_FUNC il void cmin(T & x, const T &y){ x = min(x, y); }
template<typename T> CONSTEXPR_FUNC il void cmax(T & x, const T &y){ x = max(x, y); }
template<typename T ENABLE_IF_INT> il void read(T &x){ x = 0; int f = 1; int c = getchar(); while (!isdigit(c)) { if (c == '-') f = -1; c = getchar(); } while (isdigit(c)) { x = x * 10 + c - '0'; c = getchar(); } x *= f; }
il void read(string &s) { s = ""; char ch = getchar(); while (isspace(ch)) ch = getchar(); while (!isspace(ch)) s += ch, ch = getchar(); }
il void read(char *s) { int ch = getchar(), p = 0; while (isspace(ch)) ch = getchar(); while (!isspace(ch)) s[p++] = ch, ch = getchar(); }
template<typename T, typename ... Args> il void read(T &x, Args &... y){ read(x), read(y...); }
// clang-format on

// File head end

namespace {
constexpr ll MAXN = 105;
int n, k, a[MAXN];
vector<ll> c[MAXN], best[MAXN];
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
ll calc() {
  ll ans = 0;
  For(i, 0, k - 1) ans += best[i].back() - best[i][0];
  return ans;
}
il bool ok(int x, int p, int y, int q) {
  assert(x != y && c[x][p] != c[y][q]);
  for (ll i : c[x])
    if (i == c[y][q])
      return 0;
  for (ll i : c[y])
    if (i == c[x][p])
      return 0;
  return 1;
}
il void swp(int u, int &p, int v, int &q) {
  auto x = c[u][p], y = c[v][q];
  swap(c[u][p], c[v][q]);
  sort(c[u].begin(), c[u].end());
  sort(c[v].begin(), c[v].end());
  For(i, 0, n / k - 1) if (c[u][i] == y) p = i;
  For(i, 0, n / k - 1) if (c[v][i] == x) q = i;
  // if (c[u][p] > c[u].back())
  //   swap(c[u][p], c[u].back()), p = n / k - 1;
  // if (c[v][q] > c[v].back())
  //   swap(c[v][q], c[v].back()), q = n / k - 1;
  // if (c[u][p] < c[u][0])
  //   swap(c[u][p], c[u][0]), p = 0;
  // if (c[v][q] < c[v][0])
  //   swap(c[v][q], c[v][0]), q = 0;
}

il void Main() {
  FileIO("diyiti");
  read(n, k);
  For(i, 1, n) read(a[i]);
  sort(a + 1, a + 1 + n);

  int __id = 0;
  for (int i = 1, j = 1; i <= n; i++) {
    if (a[i] != a[j])
      j = i;
    if (i - j + 1 > k)
      return cout << "-1\n", void();
    best[__id].eb(a[i]), (__id += 1) %= k;
  }

  db Temp = 1e7;
  ll ans = calc(), cur = ans, qwq = ans;

  if (n / k == 1) {
    cout << 0 << '\n';
    return;
  }

  ll __cnt = 0;

  while (1. * clock() / CLOCKS_PER_SEC <= 1.35) {
    cur = qwq;
    copy(best, best + k, c);
    Temp = 1e4;
    __cnt++;
    while (Temp >= 1e-7) {
      int u = rnd() % k, v = rnd() % k;
      if (u == v)
        continue;
      int p = rnd() % (n / k), q = rnd() % (n / k);
      while (c[u][p] == c[v][q])
        p = rnd() % (n / k), q = rnd() % (n / k);
      if (ok(u, p, v, q)) {
        ll dlt = 0;
        dlt -= c[u].back() - c[u][0] + c[v].back() - c[v][0];
        swp(u, p, v, q);
        dlt += c[u].back() - c[u][0] + c[v].back() - c[v][0];
        if (dlt < 0) {
          cur += dlt;
          if (ans > cur)
            ans = cur;
        } else if (rnd() <= UINT_MAX * exp(-dlt / Temp))
          cur += dlt;
        else
          swp(u, p, v, q);
      }
      Temp *= 0.997;
    }
  }

  cout << ans << '\n';
  cerr << __cnt << '\n';
}
} // namespace

signed main() { return Main(), 0; }
